package com.xayy.health.ds.service.impl;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.BeanUtil;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.base.utils.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xayy.health.core.config.param.ExternalParam;
import com.xayy.health.mgt.model.SatelliteCode;
import com.xayy.health.mgt.model.SatelliteNode;
import com.xayy.health.mgt.model.vo.tupu.SatelliteApiVo;
import com.xayy.health.mgt.model.vo.tupu.SatelliteInfoVo;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xayy.health.ds.model.DsSatelliteInfo;
import com.xayy.health.ds.service.DsSatelliteInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsSatelliteInfoService实现类
 * @Auther:liuyuan
 * @Date:2025-07-08
 */
@Slf4j
@Service
public class DsSatelliteInfoServiceImpl extends BaseServiceImpl<DsSatelliteInfo> implements DsSatelliteInfoService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "TB_SATELLITE_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }
    @Autowired
    private ExternalParam externalParam;

    /**
     * 获取节点树左侧信第三方接口
     * @param pageNum  页码
     * @param pageSize 页数
     * @param xingType 卫星类型
     * @return 结果日志
     */
    @Override
    public CheckResult<?> getLeftTreeByOther(Integer pageNum, Integer pageSize, String xingType) {

        if (Boolean.TRUE.equals(externalParam.ourOrotherApi)) {
            List<SatelliteNode> list = new ArrayList<>();
            if (xingType.equals("TongXin")) {
                SatelliteNode satelliteNode = new SatelliteNode();
                satelliteNode.setId(11034L);
                satelliteNode.setSatelliteId(11034L);
                satelliteNode.setSatelliteNewId("11034");
                satelliteNode.setName("XXXX04星");
                satelliteNode.setCode("TL104");
                satelliteNode.setXing("XXXX04星");
                list.add(satelliteNode);
            }
            if (xingType.equals("ZhongJi")) {
                SatelliteNode satelliteNode = new SatelliteNode();
                satelliteNode.setId(11035L);
                satelliteNode.setSatelliteId(11035L);
                satelliteNode.setSatelliteNewId("11035");
                satelliteNode.setName("XXXX01");
                satelliteNode.setCode("ST203");
                satelliteNode.setXing("XXXX01星");
                list.add(satelliteNode);
            }
            return CheckResult.success(list);
        } else {
            List<SatelliteNode> satelliteNodes = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("xingType", xingType);
            HttpResponse getResponse =
                    HttpUtil.createRequest(Method.GET, externalParam.getSatelliteUrl()).form(map).execute();
            if (getResponse.getStatus() != 200) {
                log.error("获取卫星信息列表接口异常");
                return CheckResult.fail("获取卫星信息列表接口异常");
            }
            Object data = JSON.parseObject(getResponse.body()).get("data");
            SatelliteApiVo satelliteApiVo = JSON.parseObject(data.toString(), SatelliteApiVo.class);
            List<SatelliteInfoVo> records = satelliteApiVo.getRecords();
            records.forEach(x -> {
                if (BeanUtil.isEmpty(x.getGraphId())) {
                    return;
                }
                SatelliteNode satelliteNode = new SatelliteNode();
                satelliteNode.setId(Long.valueOf(x.getGraphId()));
                satelliteNode.setSatelliteId(Long.valueOf(x.getGraphId()));
                satelliteNode.setSatelliteNewId(x.getSatelliteId());
                satelliteNode.setName(x.getXing());
                satelliteNode.setCode(x.getCode());
                satelliteNode.setXing(x.getXing());
                satelliteNodes.add(satelliteNode);
            });
//            this.updateSatelliteId(satelliteNodes);
            return CheckResult.success(satelliteNodes);
        }
    }

}
